import { MysqlBin, MysqlNative } from 'coa-mysql'
import Manager from "../../entity/Manager";
import { mysqlBin } from "../DataExecuter";
import Utils from "../../utils/Utils";

// 通过基类初始化
export const managerMapper = new (class extends MysqlNative<Manager> {
    private tableName = 'manager';

    constructor() {
        super(
            {
                name: 'manager', // 表名，默认会转化为下划线(snackCase)形式，如 User->user UserPhoto->user_photo
                title: '用户表', // 表的备注名称
                scheme: new Manager(), // 表的默认结构
                pick: ['name', 'mobile'], // 查询列表时显示的字段信息
            },
            // 绑定配置实例bin
            mysqlBin
        )
    }

    async findByVo(vo:Manager,columns?:string[]): Promise<Manager[] | null>{
        if(vo){
            if(!columns){
                columns = Object.keys(new Manager());
            }
            let whereInfo:any = {};
            if(vo.name){
                whereInfo.name = vo.name;
            }
            if(vo.mobile){
                whereInfo.mobile = vo.mobile;
            }
            if(vo.managerId){
                whereInfo.managerId = vo.managerId;
            }
            if(Object.keys(whereInfo).length > 0){
                return await mysqlBin.io.column(columns).where(whereInfo).from(this.tableName)
            }
        }
        return null;
    }

    async findAll(columns?:string[]): Promise<Manager[] | null>{
        if(!columns){
            columns = Object.keys(new Manager());
        }
        return await mysqlBin.io.column(columns).from(this.tableName)
    }

    async isExistByManagerIdOrMobile(managerId:string,mobile:string): Promise<boolean>{
        if(managerId && mobile){
            let list = await mysqlBin.io.column('managerId').where('managerId','=',managerId).orWhere('mobile','=',mobile).from(this.tableName);
            return list != null && list.length > 0
        }
        return false;
    }

    async updateByMobile(vo:Manager){
        if(Utils.isNotEmpty(vo) && Utils.isNotEmpty(vo.mobile)){
            // @ts-ignore
            return await mysqlBin.io(this.tableName).where('mobile','=',vo.mobile).update(vo)
        }
    }
})()
